const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");

const CLIPDATA_TYPE = {
    max:{
        fixedhp: 298.75,
        fixedAtt: 19.45,
        fixdefence: 23.15,
        reaction : 23.31,
        attRate: 5.83, 
        hpRate: 5.83, 
        criticalRate: 3.89,
        criticalStrike: 7.77,
        defRate: 7.29,
        charging: 6.48
    },
    mid:{
        fixedhp: 253.94,   // 209.13  239   268.88   298.75
        fixdefence: 19.675, // 16.2   18.52  20.83  23.15
        fixedAtt: 16.535,   //  13.62   15.56   17.51  19.45
        reaction : 19.815,  // 16.32   18.65  20.98  23.31
        attRate: 4.955,  // 4.08  4.66  5.25  5.83
        hpRate: 4.955,   // 4.08  4.66  5.25  5.83
        criticalRate: 3.305,   //  2.72  3.11   3.5   3.89
        criticalStrike: 6.605,  //  5.44  6.22  6.99  7.77
        defRate: 6.195,   //  5.1  5.83  6.56   7.29
        charging: 5.505  //  4.53   5.18   5.83   6.48
    }
}


const CLIPDATA = CLIPDATA_TYPE.mid


const CONFIG = {
    chance: 20,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        criticalStrike: 62.2,
        dmgRate: 46.6,
        attRate: 46.6,
    },
    clips: ["attRate", "charging", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["attRate", "charging", "criticalRate", "criticalStrike"],
        ["attRate", "charging", "criticalRate", "criticalStrike"],
        ["fixedAtt", "charging", "criticalRate", "criticalStrike"],
        ["attRate", "charging", "criticalRate", "criticalStrike"],
        ["fixedAtt", "charging", "attRate", "criticalRate"]
    ],
    reactionK : 1.5,
    buffs: [  // 夜莫万 钟
        {attRate: 18 * 2} // 角斗+追忆
        // { charging: 20}, // 充能套
        // { dmgRate:15, hpRate: 20}, // 2+2
        // { dmgRate:48, jk: 40, admgRate: 32}, // 万叶
        // { mnCrRate:15, mnDmgRate: 60}, // mn
        // { jk: 20, dmgRate: 35}, // 钟离 
        // { hpRate:25}, // 双水
    ]
}

let charaters = {
    units : {
        name: "双马尾风辅", 
        charging: 100,
        defence: 627.92,
        attRate:0,
        hp:9570,
        base: 196.47,    
        criticalRate: 5,   
        attRate: 24, 
        criticalStrike: 50,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}


/**
    lv10蓄力倍率223.2%
    lv13 e  释放316.2%   风压塌陷风涡 229.5% 牵引敌人 天赋解锁降低风抗
    lv13 q  烈风波 453.9%/hit  祈风之赐 风伤加成 38.25  诡风之祸 降低风抗40   持续/冷却 12/20 能量80
    天赋 祈风之赐效果下的角色 根据双马尾攻击的 32% 提高风普攻重击下落eq的伤害值  每0.8秒至多产生一次效果，
    该效果将在「祈风之赐」持续时间结束或生效1次后消失。

    1命  e后蓄力会发射两枚飓烈箭
    2命  q持续时间延长6s
    3命  e + 3
    4命  风压坍陷恢复能量
    5命  q + 3
    6命  处于抟风秘道的「祈风之赐」效果影响下的角色，造成风元素伤害时的暴击伤害提升40%；
    处于「祈风之赐」效果影响下的当前场上角色造成伤害时，将为该敌人施加「风压坍陷」。此效果3秒至多触发一次，队伍中所有角色共享冷却时间。
 */
calc(calc_clips_initial());

function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        if(weapon.weapontype == "bow") {
            charactersDmg.push(getDMG([charaters.units,weapon,{fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
                count++;
                data = objValueAdd([data, ...CONFIG.buffs]);
                data.hp = data.hp*(1 + (data.hpRate||0)/100 + 0.06 + 0) + 4780;
               
                if(weapon.skill) { weapon.skill(data); }
               
                data.jk = (data.jk||0) + 40
                data.dmgRate += 38.25
                data.criticalStrike += 40
                
                let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:data.reactionDmgAdd || 0 }
                let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
    
                let AZDmg = getDmg({ data:{...data, 
                }, rate:2.232}).qw 
                * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, 0); //
                let EDmg = getDmg({ data:{...data, 
                }, rate:3.162}).qw 
                * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, 0); //
                let E2Dmg = getDmg({ data:{...data, 
                }, rate:2.295}).qw 
                * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, 0); //
                let QDmg = getDmg({ data:{...data, 
                }, rate:4.539}).qw 
                * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, 0); //
                let dmgZhidmg = getDmg({ data:{...data, 
                    dmgZhi:(data.dmgZhi||0) + attack*0.32
                }, rate:0}).qw 
                * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, 0); //
    
                if(data.phyRate) { // 物理特效伤害
                    data.phyDmg = getDmg({data:{...data, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
                }
    
                let point = EDmg + AZDmg * 2 + E2Dmg * 2 + QDmg*4 + dmgZhidmg*4+(data.phyDmg||0);
                return {point, data, second, attack, dmg:{EDmg, AZDmg,E2Dmg,QDmg,dmgZhidmg,attack }}
            }));
        }
    })
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point);
    });
    console.log(charactersDmg[1]);
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(5)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                })
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return  getRet(panel, second)
    })
    _ret.sort((b,a)=> a.point-b.point)
    return _ret[0]
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
